<template>
  <!-- 所有item都展示两个东西，一个图标，一个文字 -->
  <div class="tabbar-item" @click="itemClick" :style="activeStyle">
    <!-- 提供 两个具名插槽 -->
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <div>
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  props: {
    path: {
      type: String,
    },
    activeColor: {
      type: String,
      default: "#ff5777",
    },
  },
  computed: {
    isActive() {
      // indexOf 如果返回 -1，表示前面字符串中不含有后面字符串
      return this.$route.path.indexOf(this.path) !== -1;
    },
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    },
  },
  methods: {
    itemClick() {
      this.$router.push(this.path);
    },
  },
};
</script>

<style lang="less" scoped>
.tabbar-item {
  flex: 1;
  text-align: center;
  // 很多app/小程序 tabbar 高度都是 49px
  height: 49px;
  img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle;
    margin-bottom: 2px;
  }
}
</style>